<?php
/**
 * @package panel
 * @subpackage panel.environment.db
 */
/**
 * Overload of maf version to add
 * CP db layer features.
 * @author Dmitry Bakaleinik (code)
 * @method panel__db__select setFrom()
 * @method panel__db__select addSelect()
 * @method panel__db__select addSelectConcat()
 * @method panel__db__select addSelectSubquery()
 * @method panel__db__select setSelect()
 * @method panel__db__select isSubquery()
 * @method panel__db__select setDistinct()
 * @method panel__db__select addInnerJoin()
 * @method panel__db__select setLimit()
 * @method panel__db__select setWhere()
 * @method panel__db__select addWhere()
 */
class panel__db__select
	extends maf__sqlgen__select {

	/**
	 * @return panel__db__select
	 */
	static public function create() {

		return new self;

	}	# Function

	public function execute() {

		return p()->db()->querySelect($this);

	}	# Function

	public function fetchAll() {

		return p()->db()->querySelect($this);

	}	# Function

	/**
	 * @param string $_eid_ DB ID Field
	 * @param string $_etype_ Etype
	 * @param string $_prefix_ Prefix for fields
	 * @return panel__db__select
	 */
	public function addSelectEFields( $_eid_, $_etype_, $_prefix_ = "" ) {

		$this->addSelect($_eid_, "{$_prefix_}eid");
		$this->addSelectString($_etype_, "{$_prefix_}etype");
		$this->addSelectUID($_eid_, $_etype_, $_prefix_);

		return $this;

	}	# Function

	/**
	 * @param string $_eid_ DB ID Field
	 * @param string $_etype_ Etype
	 * @param string $_prefix_ Prefix for fields
	 * @return panel__db__select
	 */
	public function addSelectUID( $_eid_, $_etype_, $_prefix_ = "" ) {

		$this->addSelectConcat(
			array($_etype_, "_", array($_eid_, TRUE)),
			"{$_prefix_}uid"
		);

		return $this;

	}	# Function

	public function extractValue() {

		$r = self::execute();
		$a = func_get_args();

		array_unshift($a, $r);

		return call_user_func_array( array("maf__helpers__array", "getElement"), $a );

	}	# Function

	public function extractColumn($_column_) {

		$r = self::execute();
		return maf__helpers__array::extractColumn($r, $_column_);

	}	# Function

	public function extractFirst() {

		$r = self::execute();
		return current($r);

	}	# Function

}	# Class

/* EOF */